package com.zhuizikejithreeig.cn.threeigkeji.mianinfo;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhuizikejithreeig.cn.adapter.WalletActivityAdapter;
import com.zhuizikejithreeig.cn.been.MesBeen;
import com.zhuizikejithreeig.cn.been.WalletBeen;
import com.zhuizikejithreeig.cn.contect.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;
import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import okhttp3.Call;
import okhttp3.MediaType;

public class WalletActivity extends BasicActivity implements PlatformActionListener {

    private TextView              mMoney;
    private TextView              mTakemoney;
    private RecyclerView          mRecycleview;
    private WalletActivityAdapter mAdapter;
    private List<WalletBeen.DataBean> mList = new ArrayList<>();
    private SmartRefreshLayout mRefreshLayout;
    private TextView           mText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_wallet);
        setLeftShow(3, R.mipmap.message);
        //点击小人去登录界面
        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mMoney = (TextView) findViewById(R.id.money);
        mText = (TextView) findViewById(R.id.text);
        mTakemoney = (TextView) findViewById(R.id.takemoney);
        mRecycleview = (RecyclerView) findViewById(R.id.recycleview);


        //下拉刷新
        mRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smartrefreshlayout);
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(WalletActivity.this));
        //下拉刷新
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                mRefreshLayout.finishRefresh();
            }
        });

        //上拉加载更多
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mRefreshLayout.finishLoadmore();
            }
        });

        //点击提取现金
        mTakemoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isConnected()){
                    takeShodle();
                }
                //showPermission();
            }
        });

        LinearLayoutManager manager = new LinearLayoutManager(WalletActivity.this);
        mRecycleview.setLayoutManager(manager);
        mAdapter = new WalletActivityAdapter(mList);
        mRecycleview.setAdapter(mAdapter);

        if (Utils.netConnected(WalletActivity.this)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    haveMoney();
                }
            }).start();

        }
    }

    private void showPermission() {


        Platform weibo = ShareSDK.getPlatform(Wechat.NAME);
        weibo.SSOSetting(false);  //设置false表示使用SSO授权方式
        weibo.setPlatformActionListener(WalletActivity.this); // 设置分享事件回调

        weibo.authorize();//单独授权
        weibo.showUser(null);//授权并获取用户信息


    }

    private void takeShodle() {
//        CompontApplayCation.phone
        HashMap<String, String> map = new HashMap<>();
        map.put("phone","18221884661");
        String json = GsonUtil.parseMapToJson(map);
        showProgrees(getResources().getString(R.string.string75));
        OkHttpUtils
                .postString()
                .url(UriContect.DRAW)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        hideProgree();
                        ToastMake.showToast(getResources().getString(R.string.string58));
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        hideProgree();
                        MesBeen mesBeen = GsonUtil.parseJsonToBean(response, MesBeen.class);
                        if (mesBeen == null) {
                            ToastMake.showToast(getResources().getString(R.string.string58));
                            return;
                        }
                        if (!TextUtils.equals(mesBeen.getStatus(), "1")) {
                            ToastMake.showToast(mesBeen.getMsg());
                            return;
                        } else {
                           ToastMake.showToast(mesBeen.getMsg());
                        }
                    }
                });


    }

    private void haveMoney() {
        Map<String, String> map = new HashMap<>();
        map.put("phone", CompontApplayCation.phone);
        String json = GsonUtil.parseMapToJson(map);
        mList.clear();
        showProgrees("");
        OkHttpUtils
                .postString()
                .url(UriContect.RECORD)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        hideProgree();
                        ToastMake.showToast("获取数据失败");
                        Utils.runOnUIThread(new Runnable() {
                            @Override
                            public void run() {
                                mRecycleview.setVisibility(View.VISIBLE);
                                mText.setVisibility(View.GONE);
                            }
                        });
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        hideProgree();
                        final WalletBeen bean = GsonUtil.parseJsonToBean(response, WalletBeen.class);
                        if (bean==null){
                            ToastMake.showToast("获取数据失败");
                            return;
                        }
                        if (!TextUtils.equals(bean.getStatus(),"1")){
                            ToastMake.showToast(bean.getMsg());
                            return;
                        }else {
                            Utils.runOnUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    mMoney.setText(bean.getSum());
                                    List<WalletBeen.DataBean> data = bean.getData();
                                    if(data.size()==0){
                                        mRecycleview.setVisibility(View.GONE);
                                        mText.setVisibility(View.VISIBLE);
                                    }else {
                                        mRecycleview.setVisibility(View.VISIBLE);
                                        mText.setVisibility(View.GONE);
                                        mList.addAll(data);
                                        mAdapter.notifyDataSetChanged();
                                    }
                                }
                            });
                        }
                    }
                });
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        String accessToken = platform.getDb().getToken(); // 获取授权token
        String openId = platform.getDb().getUserId(); // 获取用户在此平台的ID
        String nickname = platform.getDb().getUserName(); // 获取用户昵称
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {

    }

    @Override
    public void onCancel(Platform platform, int i) {

    }
}
